import { observable, action } from 'mobx';
import { User } from '@/type/Auth';
import userApi from '@/api/userApi';
import { PaginateConfig } from '@/type/Shared';

export default class UserStore {
    @observable users: User[] = [];

    @action
    public async getUsers(config: PaginateConfig) {
        const paginated = await userApi.getUsers(config);
        this.users = paginated.data;
        return paginated;
    }

    @action
    public async getUser(id: number) {
        return userApi.getUser(id);
    }

    @action
    public async saveUser(user: User, roles: number[]) {
        if (user.id) return this.updateUser(user, roles);
        return this.createUser(user, roles);
    }

    @action
    public async createUser(user: User, roles: number[]) {
        return userApi.createUser(user, roles);
    }

    @action
    public async updateUser(user: User, roles: number[]) {
        delete user.username;
        return userApi.updateUser(user, roles);
    }

    @action
    public async deleteUser(id: number) {
        return userApi.deleteUser(id);
    }

    @action
    public async logoutAllUser() {
        return userApi.logoutAllUser();
    }

    @action
    public async logoutUser(id: number) {
        return userApi.logoutUser(id);
    }

    @action
    public async generateLoginOtp() {
        return userApi.generateLoginOtp();
    }

    @action
    public async clearLoginOtp() {
        return userApi.clearLoginOtp();
    }
}
